<?php

class ItemTypeController extends \BaseController {

    /**
     * Display a listing of the resource.
     *
     * @return Response
     */
    public function index()
    {
        $item_types = ItemType::whereHas('group', function($q) {
                $q->where('group_id', '=', Auth::user()->group_id);
            })->get();
        
        return View::make('crud.index', array (
            'objects'=>$item_types, 
            'title'=>'item types',
            'columns'=>ItemType::indexColumns(),
            'table'=>'item_types'
        ));
        
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return Response
     */
    public function create()
    {        
        return View::make('crud.form')->with(array (
            'title'=> 'create item type',
            'fields'=>ItemType::editFields(),
            'table'=>'item_types'
        ));
        
    }
    
    /**
     * Store a newly created resource in storage.
     *
     * @return Response
     */
    public function store()
    {
        $validator = Validator::make(
            Input::all(),
            array (
                'name' => 'required|uniqueingroup:ItemType',
                'item_category_id' => 'required|ingroup:ItemCategory'
            )
        );
        
        if ($validator->fails()) {
            return Redirect::to('item_types/create')->withErrors($validator)->withInput();       
        } else {
            $group = Group::find(Auth::user()->group_id);
            $cat = ItemCategory::find(Input::get('item_category_id'));
            $item_type = new ItemType;
            $item_type->name = Input::get('name');            
            $item_type->group()->associate($group);
            $item_type->itemCategory()->associate($cat);
            $item_type->save();
        }
        
        Session::flash('message', 'Successfully created item ' . $item_type->id);

        return Redirect::to('/item_types');        
    }
    
    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return Response
     */
    public function show($id)
    {
        // get the item type
        $object = ItemType::find($id);
        $this->checkModel($object);
        
        // are there items using this type?
        $allow_delete = !(Lot::where('item_type_id', '=', $id)->count());
        
        // show the view and pass the object to it
        return View::make('crud.show')
            ->with(array ('object'=>$object, 'title'=>$object->showTitle(), 'allow_delete'=>$allow_delete));
    }
    
    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return Response
     */
    public function edit($id)
    {
        // get the item type
        $object = ItemType::find($id);
        $this->checkModel($object);

        return View::make('crud.form')->with(array (
            'title'=>'edit '.$object->name, 
            'object' => $object,
            'table' => 'item_types',
            'fields'=>ItemType::editFields($object),
        ));

    }
    
    /**
     * Update the specified resource in storage.
     *
     * @param  int  $id
     * @return Response
     */
    public function update($id)
    {
        // get the item type
        $item_type = ItemType::find($id);
        $this->checkModel($item_type);

         $validator = Validator::make(
            Input::all(),
            array ('name' => 'required')
        );
        
        if ($validator->fails()) {
            return Redirect::to('item_types/' . $id)->withErrors($validator)->withInput();       
        } else {
            $cat = ItemCategory::find(Input::get('item_category_id'));
            $item_type->name = Input::get('name');
            $item_type->itemCategory()->associate($cat);
            $item_type->save();
        }
        
        Session::flash('message', 'Successfully updated item ' . $item_type->id);

        return Redirect::to('/item_types');
       
    }
    
    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return Response
     */
    public function destroy($id)
    {
        // get the item type
        $item_type = ItemType::find($id);
        if ($item_type) {
            $this->checkModel($item_type);

            ItemType::destroy($id);
        }

        return Redirect::to('/item_types');        
    }

    public function select()
    {
        $cat_id = Input::get('item_category_id');
        $cat = ItemCategory::find($cat_id);
        $this->checkModel($cat);
        $options = $cat->itemTypes->sortBy('name')->lists('name', 'id');

        return View::make('ajax_select')->with(
            array ('options'=>$options)
        );
    }
}